using StackExchange.Redis;
using Microsoft.Extensions.Configuration;
using System;

namespace RabbitTraceability.API.Application.Utils
{
    /// <summary>
    /// Redis操作帮助类2，支持连接和查询
    /// </summary>
    public class RedisHelper2
    {
        private readonly ConnectionMultiplexer _connection;
        private readonly IDatabase _db;

        /// <summary>
        /// 构造函数，初始化Redis连接
        /// </summary>
        /// <param name="configuration">应用配置</param>
        public RedisHelper2(IConfiguration configuration)
        {
            var connectionString = configuration["Redis:Configuration"];
            if (string.IsNullOrWhiteSpace(connectionString))
                throw new ArgumentException("Redis连接字符串不能为空", nameof(configuration));
            _connection = ConnectionMultiplexer.Connect(connectionString);
            _db = _connection.GetDatabase();
        }

        /// <summary>
        /// 查询指定key的值
        /// </summary>
        /// <param name="key">Redis键</param>
        /// <returns>对应的值，如果不存在则返回null</returns>
        public string? GetValue(string key)
        {
            return _db.StringGet(key);
        }
    }
} 